﻿using huqiang.Core.HGUI;
using huqiang.UIModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

public class HotFixPage : UIPage
{
    bool Hot;
    public override void Initial(UIElement parent, object dat = null)
    {
        base.Initial(parent, dat);
        HotData hot = dat as HotData;
        byte[] ui;
        if (LocalSetting.IsEditor)
        { 
            ui = File.ReadAllBytes("Assets/AssetsBundle/hotUI.bytes"); 
        }
        else
        {
            string path = AssetsVersionControl.GetAssetsPath("hotui");
            if(path=="")
            {
                ui = File.ReadAllBytes("Assets/AssetsBundle/hotUI.bytes");
            }
            else
            {
                AssetBundle ab = AssetBundle.LoadFromFile(path);
                ui = ab.LoadAsset<TextAsset>("hotui.bytes").bytes;
                ab.Unload(true);
            }
            
        }
        HGUIManager.LoadModels(ui, "hotUI");
        if (hot.hot)
        {
            HotFix.Instance.LoadDll();
            HotFix.Instance.Initial();
            HotFix.Instance.Start(Parent, "Start", dat);
        }
        else
        {
            huqiang.HotUIModel.UIPage.UIRoot = parent;
            huqiang.HotUIModel.UIPage.LoadPage<HotPage.StartPage>();
        }
    }
    public override void Show(object dat = null)
    {
        if (Hot)
            HotFix.Instance.Start(Parent, "Start", dat);
        else huqiang.HotUIModel.UIPage.LoadPage<HotPage.StartPage>();
    }
    public override void Update(float time)
    {
        if (Hot)
            HotFix.Instance.RuntimeUpdate(time);
        else if (huqiang.HotUIModel.UIPage.CurrentPage != null)
        {
            huqiang.HotUIModel.UIPage.CurrentPage.Update(time);
        }
    }
    public override void ReSize()
    {
        if (Hot)
            HotFix.Instance.RuntimeReSize();
        else if (huqiang.HotUIModel.UIPage.CurrentPage != null)
            huqiang.HotUIModel.UIPage.CurrentPage.ReSize();
    }
    public override void Dispose()
    {
        if (Hot)
            HotFix.Instance.RuntimeDispose();
        else if (huqiang.HotUIModel.UIPage.CurrentPage != null)
            huqiang.HotUIModel.UIPage.CurrentPage.Dispose();
    }
}
